#include "pathStack.h"

int InitStack(pathStack_t *pathStack){
    pathStack->pTop=NULL;
    pathStack->length=0;
    return 0;
}

bool isEmptyStack(pathStack_t *pathStack){
    if(pathStack->length==0){
        return true;
    }else{
        return false;
    }
}

int PushStack(pathStack_t *pathStack,char *dir){
    Node_t *pNew=(Node_t*)malloc(sizeof(Node_t));
    strcpy(pNew->dir,dir);

    if(isEmptyStack(pathStack)){
        pathStack->pTop=pNew;
        pNew->pNext=NULL;
        pathStack->length=1;
        return 0;
    }
    pNew->pNext=pathStack->pTop;
    pathStack->pTop=pNew;
    pathStack->length++;

    return 0;
}
int PopStack(pathStack_t *pathStack){
    if(isEmptyStack(pathStack)){
        printf("Empty Stack!\n");
        NETDISK_LOG_DEBUG
        return -1;
    }
    Node_t *p=pathStack->pTop;
    pathStack->pTop=pathStack->pTop->pNext;
    pathStack->length--;
    free(p);
    p=NULL;
    return 0;
}

int peekStack(pathStack_t *pathStack){
    if(isEmptyStack(pathStack)){
        printf("Empty Stack!\n");
        NETDISK_LOG_DEBUG
        return -1;
    }
    printf("%s\n",pathStack->pTop->dir);
    fflush(stdout);
    return 0;
}
int clearStack(pathStack_t *pathStack){
    while(!isEmptyStack(pathStack)){
        PopStack(pathStack);
    }
    return 0;
}
